package com.itheima.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.store.domain.BookBorrow;
import com.itheima.store.domain.Order;
import com.itheima.store.domain.OrderItem;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.User;
import com.itheima.store.service.BookBorrowService;
import com.itheima.store.service.OrderService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

/**
 * Servlet implementation class AdminBookBorrowServlet
 */
public class AdminBookBorrowServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	public String findAllApp(HttpServletRequest req ,HttpServletResponse resp){
		// 接收参数:					
		Integer currPage = Integer.parseInt(req.getParameter("currPage")==null?"1":req.getParameter("currPage"));
		Integer bstatus = Integer.parseInt(req.getParameter("bstatus")==null?"3":req.getParameter("bstatus"));
		String flag = req.getParameter("flag");

		// 调用业务层:
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		PageBean<BookBorrow> pageBean = null;
		try {
			pageBean = bbs.findAllApp(bstatus,currPage);
			
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		req.setAttribute("flag", flag);
		req.setAttribute("bstatus", bstatus);
		
				
		req.setAttribute("pageBean", pageBean);
		return "admin/order/list.jsp";
	}
	
	
	public String findOrderItem(HttpServletRequest req ,HttpServletResponse resp){
		try {
			//获取参数
			String bookBorrowing_id = req.getParameter("bookBorrowing_id");
			BookBorrowService bookBorrowService  = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
			BookBorrow bb = bookBorrowService.findAppBybookBorrId(bookBorrowing_id);
			

			JSONArray jsonArray = JSONArray.fromObject(bb);
			resp.getWriter().println(jsonArray.toString());
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		return null;
	}
	
	public String send(HttpServletRequest req ,HttpServletResponse resp){
		String bookBorrowing_id = req.getParameter("bookBorrowing_id");
		String flag = req.getParameter("flag");
		BookBorrowService bookBorrowService  = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		try {
			bookBorrowService.updateBookBorrowStatus(bookBorrowing_id,flag);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			/*if("0".equals(flag)){
				resp.sendRedirect(req.getContextPath()+"/AdminBookBorrowServlet?method=findAllApp&currPage=1&bstatus=4&flag=0");
			}else*/ if("1".equals(flag)){
				resp.sendRedirect(req.getContextPath()+"/AdminBookBorrowServlet?method=findAllApp&currPage=1&bstatus=0&flag=1");
			}if("3".equals(flag)){
				resp.sendRedirect(req.getContextPath()+"/BookBorrowItemsServlet?method=findByUserId&currPage=1");
			}else{
				resp.sendRedirect(req.getContextPath()+"/AdminBookBorrowServlet?method=findAllApp&currPage=1&bstatus=4&flag=0");

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	


}
